package com.zhj.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

/**
 *
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 将多数据源设置进mybatis的工厂类中
     * @return
     */
    // @Bean
    // public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    //     SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    //     sqlSessionFactoryBean.setDataSource(dataSource);
    //     sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    //     return sqlSessionFactoryBean.getObject();
    // }
    //
    // @Bean
    // public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
    //     return new SqlSessionTemplate(sqlSessionFactory);
    // }



    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType("postgresql");
        return paginationInterceptor;
    }

    @Bean
    public DataSource dataSource(DataSourceProperties dataSourceProperties) {
        MultiDataSource dataSource = new MultiDataSource();
        Map<Object, Object> datasourceMap = new HashMap<>();
        DataSource tdengineDatasource = tdengineDatasource();
        datasourceMap.put("tdengine", tdengineDatasource);
        datasourceMap.put("mysql", mysqlDatasource());
        dataSource.setDefaultTargetDataSource("tdengine");
        dataSource.setDataSourceLookup((str) -> tdengineDatasource);
        dataSource.setTargetDataSources(datasourceMap);
        return dataSource; // get connection
    }

    private DataSource tdengineDatasource() {
        DruidDataSource dataSource = new DruidDataSource();
        // jdbc properties
        dataSource.setDriverClassName(dataSource.getDriverClassName());
        String jdbcUrl = "jdbc:TAOS://tdengine-server:6030/iot?user=root&password=taosdata";
        dataSource.setDriverClassName("com.taosdata.jdbc.TSDBDriver");
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername("root");
        dataSource.setPassword("taosdata");
        // pool configurations
        dataSource.setInitialSize(10);
        dataSource.setMinIdle(10);
        dataSource.setMaxActive(10);
        dataSource.setMaxWait(30000);
        dataSource.setValidationQuery("select server_status()");
        return dataSource; // get connection
    }

    private DataSource mysqlDatasource() {
        DruidDataSource dataSource = new DruidDataSource();
        // jdbc properties
        dataSource.setDriverClassName(dataSource.getDriverClassName());
        String jdbcUrl = "jdbc:mysql://localhost:3306/iot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC";
        dataSource.setUrl(jdbcUrl);
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        // pool configurations
        dataSource.setInitialSize(10);
        dataSource.setMinIdle(10);
        dataSource.setMaxActive(10);
        dataSource.setMaxWait(30000);
        dataSource.setValidationQuery("select server_status()");
        return dataSource; // get connection
    }

}
